//
//  AZTestViewController.m
//  AZBLE
//
//  Created by AZ-Mac on 2017/6/5.
//  Copyright © 2017年 AZ-Mac. All rights reserved.
//

#import "AZTestViewController.h"

@interface AZTestViewController ()

@property (weak, nonatomic) IBOutlet UITextField *inputTF;

@property (weak, nonatomic) IBOutlet UILabel *scaleLabel;

/** 定时器 */
@property(strong,nonatomic) NSTimer *  timer;
/** 存放按钮的数组 */
@property(strong,nonatomic) NSMutableArray *  buttonTitles;
@property(strong,nonatomic) NSMutableArray *  buttons;
/** 存放按钮的数组 */
@property(assign,nonatomic) int   btnIndex;
@end

@implementation AZTestViewController
- (NSMutableArray *)buttonTitles
{
    if( _buttonTitles == nil)
    {
        _buttonTitles = [NSMutableArray array];
    }
    return  _buttonTitles;
}
- (NSMutableArray *)buttons
{
    if( _buttons == nil)
    {
        _buttons = [NSMutableArray array];
    }
    return  _buttons;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self.buttonTitles addObjectsFromArray:@[@"10->16",@"16->10",@"十进制",@"1",@"2",@"3",@"4"]];
    
    //添加所有按钮
    [self addAllBtn];
    //设置定时器为所有按钮做动画
    self.btnIndex = 0;
    self.timer = [NSTimer scheduledTimerWithTimeInterval:0.05 target:self selector:@selector(timeScheduled) userInfo:nil repeats:YES];
    
}
//十进制转16进制
- (void)tenToHexClick {
    NSString *oriString = self.inputTF.text;
    self.scaleLabel.text =  [NSString stringWithFormat:@"%08x",[oriString integerValue]];
    [self.view endEditing:YES];
}
//16进制转10进制
- (void)hexToTenClick {
    NSString *oriString = self.inputTF.text;
    NSString * temp10 = [NSString stringWithFormat:@"%lu",strtoul([oriString UTF8String],0,16)];
    self.scaleLabel.text =  [NSString stringWithFormat:@"%@",temp10];
    [self.view endEditing:YES];
}
- (void)timeScheduled
{
    if(_btnIndex == self.buttonTitles.count)
    {
        [self.timer invalidate];
        return;
    }
    UIButton *btn =  self.buttons[_btnIndex];
    
    _btnIndex++;
    
    [self btnAnim:btn];
    
}
//按钮弹上来的动画
- (void)btnAnim:(UIButton *)btn
{
    [UIView animateWithDuration:0.6 delay:0 usingSpringWithDamping:0.6 initialSpringVelocity:0 options:UIViewAnimationOptionCurveEaseIn animations:^{
        
        btn.transform = CGAffineTransformIdentity;
        
    } completion:^(BOOL finished) {
        
    }];
}
- (void)addAllBtn
{
    int col = 4;
    CGFloat oriY   = 200;
    CGFloat leftMargin   = 10;
    CGFloat rightMargin  = 10;
    CGFloat buttonW = 60;
    CGFloat buttonH = 35;
    CGFloat buttonX = 0;
    CGFloat buttonY = 0;
    CGFloat marginW  = (AZScreenW - leftMargin - rightMargin - col * buttonW) / (col - 1);
    CGFloat marginH  = marginW;
    for(int i = 0; i < self.buttonTitles.count ; i++)
    {
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        [button setTitle:self.buttonTitles[i] forState:UIControlStateNormal];
        [button setBackgroundColor:[UIColor blueColor]];
        buttonX = leftMargin + i % col * (marginW + buttonW);
        buttonY = oriY + i / col * (marginH + buttonH);
        button.frame = CGRectMake(buttonX, buttonY, buttonW, buttonH);
        //按钮偏移到屏幕底部
        button.transform = CGAffineTransformMakeTranslation(0, -self.view.bounds.size.height);
        //为每个按钮添加事件
        [button addTarget:self action:@selector(btnAction:) forControlEvents:UIControlEventTouchUpInside];
        button.tag = i;
        [self.buttons addObject:button];
        [self.view addSubview:button];
    }
}
- (void)btnAction:(UIButton *)button
{
    switch (button.tag) {
        case 0:
            [self tenToHexClick];
            break;
        case 1:
            [self hexToTenClick];
            break;
        default:
            break;
    }
}
@end
